// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package com.myplans.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.myplans.criteria.UserSearchCriteria;
import com.myplans.vo.PlanValue;
import com.myplans.vo.UserValue;

/**
 * @see com.myplans.model.User
 */
public class UserDaoImpl
    extends com.myplans.model.UserDaoBase
{
    /**
     * @see com.myplans.model.UserDao#toUserValue(com.myplans.model.User, com.myplans.vo.UserValue)
     */
    public void toUserValue(
        com.myplans.model.User source,
        com.myplans.vo.UserValue target)
    {
        // @todo verify behavior of toUserValue
        super.toUserValue(source, target);
        
        if (source.getPicture() != null) {
        	target.setPicture(new String(source.getPicture()));
        }
    }


    /**
     * @see com.myplans.model.UserDao#toUserValue(com.myplans.model.User)
     */
    public com.myplans.vo.UserValue toUserValue(final com.myplans.model.User entity)
    {
        // @todo verify behavior of toUserValue
        return super.toUserValue(entity);
    }


    /**
     * Retrieves the entity object that is associated with the specified value object
     * from the object store. If no such entity object exists in the object store,
     * a new, blank entity is created
     */
    private com.myplans.model.User loadUserFromUserValue(com.myplans.vo.UserValue userValue)
    {
    	User user = User.Factory.newInstance();
    	if (userValue.getId() != null) {
    		User userEntity = this.load(userValue.getId());
    		
    		if (userEntity != null) {
    			user = userEntity;
    		}
    	}
        return user; 
    }

    
    /**
     * @see com.myplans.model.UserDao#userValueToEntity(com.myplans.vo.UserValue)
     */
    public com.myplans.model.User userValueToEntity(com.myplans.vo.UserValue userValue)
    {
        // @todo verify behavior of userValueToEntity
        com.myplans.model.User entity = this.loadUserFromUserValue(userValue);
        this.userValueToEntity(userValue, entity, true);
        return entity;
    }


    /**
     * @see com.myplans.model.UserDao#userValueToEntity(com.myplans.vo.UserValue, com.myplans.model.User)
     */
    public void userValueToEntity(
        com.myplans.vo.UserValue source,
        com.myplans.model.User target,
        boolean copyIfNull)
    {
        // @todo verify behavior of userValueToEntity
        super.userValueToEntity(source, target, copyIfNull);
        
        if (source.getPicture() != null) {
        	target.setPicture(source.getPicture().getBytes());
        }
        target.setId(source.getId());
    }


	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected List<UserValue> handleGetUsers(UserSearchCriteria searchCriteria)
			throws Exception {
		Criteria criteria = getSession().createCriteria(User.class);
		
		if (searchCriteria.getEmail() != null) {
			criteria.add(Restrictions.eq("email", searchCriteria.getEmail()));
		}
		if (searchCriteria.getPassword() != null) {
			criteria.add(Restrictions.eq("password", searchCriteria.getPassword()));
		}
		if (searchCriteria.getPlan() != null) {
			Criteria planCriteria = criteria.createCriteria("plans");
			planCriteria.add(Restrictions.eq("id", searchCriteria.getPlan().getId()));
		}
		
		List result = criteria.list();
		toUserValueCollection(result);
		return (List<UserValue>)result;
	}


	@SuppressWarnings("unchecked")
	@Override
	protected UserValue handleCreatePlanForUser(PlanValue planValue,
			UserValue userValue) throws Exception {
		User userEntity = load(userValue.getId());
		
		userEntity.getPlans().add(getPlanDao().planValueToEntity(planValue));
		
		this.update(userEntity);
		
		return userValue;
	}


	@SuppressWarnings("rawtypes")
	@Override
	protected List<PlanValue> handleGetPlansForUser(UserValue userValue)
			throws Exception {
		ArrayList<PlanValue> planValues = new ArrayList<PlanValue>();
		
		Iterator iterator = load(userValue.getId()).getPlans().iterator();
		
		while (iterator.hasNext()) {
			planValues.add(getPlanDao().toPlanValue((Plan)iterator.next()));
		}
		
		return planValues;
	}


	@SuppressWarnings("rawtypes")
	@Override
	protected List<UserValue> handleGetSubscriptionsForUser(UserValue user)
			throws Exception {
		ArrayList<UserValue> userValues = new ArrayList<UserValue>();
		
		Iterator iterator = load(user.getId()).getSubscriptions().iterator();
		
		while (iterator.hasNext()) {
			userValues.add(toUserValue(((User)iterator.next())));
		}
		
		return userValues;
	}


	@SuppressWarnings("unchecked")
	@Override
	protected UserValue handleCreateSubscriptionForUser(UserValue subscription,
			UserValue user) throws Exception {
		User userEntity = load(user.getId());
		
		userEntity.getSubscriptions().add(userValueToEntity(subscription));
		
		this.update(userEntity);
		
		return user;
	}


	@Override
	protected UserValue handleDeleteSubscriptionForUser(UserValue subscription,
			UserValue user) throws Exception {
		User userEntity = load(user.getId());
		
		userEntity.getSubscriptions().remove(load(subscription.getId()));
		
		return user;
	}

}